/*
	FileName : GF_WFL_PI.c
	Description :
		GF: General Functions;
		WFL: Work For Living;
		PI: Platform Independent;
	
	Created on 2017.12.09 by WFL.

	Revision History:
*/


#include "GF_WFL_PI.h"
//----------------------Memory GF------------------------------
//LittleEndian
uint32_t Mem_LE_Get32(uint8_t *bufAddr, uint16_t offset)
{
	uint8_t *addr;
	uint32_t returnValue = 0;
	
	addr = bufAddr + offset;
	
	returnValue += addr[3];
	returnValue <<= 8;
	returnValue += addr[2];
	returnValue <<= 8;
	returnValue += addr[1];
	returnValue <<= 8;
	returnValue += addr[0];
	
	return 	returnValue;
}

//LittleEndian
void Mem_LE_Set32(uint8_t *bufAddr, uint16_t offset, uint32_t val)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	
	addr[0] = val & 0xFF;
	val >>= 8;
	addr[1] = val & 0xFF;
	val >>= 8;
	addr[2] = val & 0xFF;
	val >>= 8;
	addr[3] = val & 0xFF;
	val >>= 8;
	
	return;
}

//LittleEndian
uint16_t Mem_LE_Get16(uint8_t *bufAddr, uint16_t offset)
{
	uint8_t *addr;
	uint16_t returnValue = 0;
	
	addr = bufAddr + offset;
	
	returnValue += addr[1];
	returnValue <<= 8;
	returnValue += addr[0];
	
	return 	returnValue;
}
//BigEndian
uint16_t Mem_BE_Get16(uint8_t *bufAddr, uint16_t offset)
{
	uint8_t *addr;
	uint16_t returnValue = 0;
	
	addr = bufAddr + offset;
	
	returnValue += addr[0];
	returnValue <<= 8;
	returnValue += addr[1];
	
	return 	returnValue;
}
//LittleEndian
void Mem_LE_Set16(uint8_t *bufAddr, uint16_t offset, uint16_t val)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	
	addr[0] = val & 0xFF;
	val >>= 8;
	addr[1] = val & 0xFF;
	
	return;
}
//BigEndian
void Mem_BE_Set16(uint8_t *bufAddr, uint16_t offset, uint16_t val)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	
	addr[1] = val & 0xFF;
	val >>= 8;
	addr[0] = val & 0xFF;
	
	return;
}

//LittleEndian
uint8_t Mem_LE_Get8(uint8_t *bufAddr, uint16_t offset)
{
	uint8_t *addr;
	uint8_t returnValue = 0;
	
	addr = bufAddr + offset;
	
	returnValue += addr[0];
	
	return 	returnValue;
}
//LittleEndian
void Mem_LE_Set8(uint8_t *bufAddr, uint16_t offset, uint8_t val)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	
	addr[0] = val & 0xFF;
	
	return;
}

uint8_t Mem_8b_Hbits(uint8_t *bufAddr, uint16_t offset, uint8_t bit)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	
	if((addr[0] & bit) == bit)
		return 1;
	return 0;
}

void Mem_8b_setAll(uint8_t *bufAddr,uint32_t offset,uint32_t len,uint8_t val)
{
	uint8_t *addr;
	
	addr = bufAddr + offset;
	while(len)
	{
		*addr = val;
		addr++;
		len--;
	}
	return;
}
uint16_t Mem_16b_getAverage(uint16_t *bufAddr,uint16_t number)
{
	uint16_t i;
	uint32_t sum = 0;
	
	for(i = 0; i< number; i++)
	{
		sum += bufAddr[i];
	}
	return (sum / number);
}
//--------------------------Buffer GF---------------------------------------------
uint32_t Buf_index_add(uint32_t start,uint32_t size,uint32_t index,uint32_t quantity)
{
	index += quantity;
	
	while (index >= (start + size))
		index -= size;
	
	return index;
}
void Buf_8b_copy(uint8_t *srcbuf,uint32_t size,uint32_t offset,uint8_t *dstBuf,uint32_t len)
{
	uint32_t i,len1,len2;
	uint8_t *ptr;
	
	len1 = size - offset;
	
	if (len <= len1){
		
		ptr = srcbuf + offset;
		for (i = 0; i < len; i++)
		{
			*dstBuf = *ptr;
			ptr++;
			dstBuf++;
		}
	}
	else{
		
		ptr = srcbuf + offset;
		for (i = 0; i < len1; i++)
		{
			*dstBuf = *ptr;
			ptr++;
			dstBuf++;
		}
		
		len2 = len - len1;
		ptr = srcbuf;
		
		for (i = 0; i < len2; i++)
		{
			*dstBuf = *ptr;
			ptr++;
			dstBuf++;
		}
	}
	return;
}
uint32_t Sec_get_leftCn(uint32_t size,uint32_t offset,uint32_t secSize)
{
	return (size - offset)/secSize;
}



///EOF
